//
//  TRFindViewController.m
//  WeiMiLan
//
//  Created by anan on 14/11/11.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRSearchProductViewController.h"
#import "QLTabPageButton.h"
#import "PPiFlatSegmentedControl.h"
#import "NSString+FontAwesome.h"

@interface TRSearchProductViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) UISearchBar *searchBar;
@property (nonatomic, weak) PPiFlatSegmentedControl *segmented2;
@end

@implementation TRSearchProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   // self.automaticallyAdjustsScrollViewInsets = NO;
    [self initNavigation];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewTappedExit:) name:@"viewTapped" object:nil];
}
- (void)viewTappedExit: (NSNotification *)noti {
    NSLog(@"%@",noti);
    NSLog(@"－－－－－接收到通知------");
    [self.view endEditing:YES];
    //  [self seachAction:searchText];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.viewPager==nil) {
        [self initViewPager];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedText:) name:@"didselectText" object:nil];
    }
}
- (void)didSelectedText: (NSNotification *)noti {
    NSLog(@"%@",noti);
    NSLog(@"－－－－－接收到通知------");
    
    NSDictionary *dict = noti.userInfo;
    UILabel *label = dict[@"label"];
    [UIView animateWithDuration:.8f animations:^{
        label.center = CGPointMake(self.searchBar.center.x, self.searchBar.center.y);
        
    } completion:^(BOOL finished) {
        self.searchBar.text = label.text;
        [label removeFromSuperview];
    }];
    
}
- (void)initViewPager
{
    CGRect rect = CGRectMake(0, 0, self.viewContent.frame.size.width, self.viewContent.frame.size.height);
    self.viewPager = [[QLViewPager alloc]initWithFrame:rect];
    self.viewPager.pageIndicatorHeight = 30;
    self.viewPager.backgroundColor = [UIColor whiteColor];//[UIColor colorWithWhite:0.092 alpha:1.000];

    self.viewPager.showPageIndicator = NO;
    [self.viewPager setup];
    [self.viewContent addSubview:self.viewPager];
  //  self.navigationItem.titleView = self.viewPager;
    self.adapter = [[TRSearchProductFragmentAdapter alloc] initWithViewController:self];
    [self.viewPager setAdapter:self.adapter];
    
    self.viewPager.delegate = self;
    [self onPageChange:self.viewPager select:self.viewPager.currentIndex];
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    self.weChatNavigationBar.height = 100;
    [self.view addSubview:self.weChatNavigationBar];
  //  self.weChatNavigationBar.titleLabel.text=@"搜索";
  //  [self.weChatNavigationBar.leftButton setBackgroundImage:@"" forState:UIControlStateNormal];
    [self.weChatNavigationBar.leftButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];

    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.frame = CGRectMake(CGRectGetMaxX(self.weChatNavigationBar.leftButton.frame), self.weChatNavigationBar.leftButton.frame.origin.y - 2, self.view.width *0.75, 32);
    searchBar.placeholder = @"搜做股票/基金/字母";
    searchBar.backgroundImage = [UIImage imageNamed:@"圆角矩形-home"];

    
    NSArray *items = @[[[PPiFlatSegmentItem alloc] initWithTitle:@"咨询" andIcon:nil],
                       [[PPiFlatSegmentItem alloc] initWithTitle:@"私募" andIcon:nil]];
    PPiFlatSegmentedControl *segmented2=[[PPiFlatSegmentedControl alloc] initWithFrame:CGRectMake(searchBar.frame.origin.x - 5 , CGRectGetMaxY(searchBar.frame) + 10, searchBar.width + 12, searchBar.height - 5)
                                                                                 items:items
                                                                          iconPosition:IconPositionRight
                                                                     andSelectionBlock:^(NSUInteger segmentIndex) {
                                                                     
                                                                         self.viewPager.currentIndex = (int)segmentIndex;
                                                                         [self onPageChange:self.viewPager select:self.viewPager.currentIndex ];
                                                                         NSLog(@"wosss---%zd",self.selectedSegmentIndex);
                                                                     }
                                                                        iconSeparation:5];
    segmented2.color=[UIColor whiteColor];
    segmented2.borderWidth=0.5;
    segmented2.borderColor=[UIColor colorWithRed:244.0f/255.0 green:67.0f/255.0 blue:60.0f/255.0 alpha:1];
    segmented2.selectedColor=[UIColor colorWithRed:244.0f/255.0 green:67.0f/255.0 blue:60.0f/255.0 alpha:1];
    segmented2.textAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                                NSForegroundColorAttributeName:[UIColor colorWithRed:244.0f/255.0 green:67.0f/255.0 blue:60.0f/255.0 alpha:1]};
    segmented2.selectedTextAttributes=@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                                        NSForegroundColorAttributeName:[UIColor whiteColor]};
    segmented2.layer.masksToBounds = YES;
    segmented2.layer.cornerRadius = segmented2.height / 2.0;
    [self.view addSubview:segmented2];
    self.segmented2 = segmented2;
    TRButton *cancelBtn = [TRButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 32 - 5, searchBar.frame.origin.y, 32, searchBar.height);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBackToHome) forControlEvents:UIControlEventTouchUpInside];
   // NSLog(@"--------%@",NSStringFromCGRect(cancelBtn.frame));
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //cancelBtn.backgroundColor = [UIColor greenColor];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [self.weChatNavigationBar.contentViews addSubview:cancelBtn];
    
    [self.weChatNavigationBar.contentViews addSubview:searchBar];
    self.searchBar = searchBar;
    self.searchBar.delegate = self;
    // [self.weChatNavigationBar.contentViews addSubview:segmented2];
    self.weChatNavigationBar.rightButton.hidden = YES;
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    if ([self.segmented2 isSelectedSegmentAtIndex:0]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchBarSearchButtonClicked" object:nil userInfo:@{@"text": searchBar.text}];
    } else {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchBarSearchButtonClickedOther" object:nil userInfo:@{@"text": searchBar.text}];
        [self.view endEditing:YES];
    }
    
    
}

- (void)cancelBackToHome {
    self.searchBar.text = @"";
    [self.searchBar resignFirstResponder];
}
-(void)exit
{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)removeTopButton{
    
}

-(void)visibleTop{
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.searchBar resignFirstResponder];
    [self.view endEditing:YES];
}
#pragma mark --QLViewPagerDelegate
- (void)onPageChange:(QLViewPager *)viewPager select:(int)select
{
    
}

@end
